<?php

class Admin_AdvController extends My_Controller_ActionAdmin {

    public function indexAction() {
        $daoAdv = new Application_Model_DAO_Adv();
        $select = $daoAdv->getAll();
        $this->view->list = $this->createPaginator($select);
    }

    public function deactivateAction() {
        $code = $this->_getParam('id');
        $form = new Admin_Form_Confirmation();

        if (!$this->_request->isPost() || !$form->isValid($_POST)) {
            if (!$this->_request->isPost()) {
                $form->getElement('id')->setValue($code);
            }
            $this->view->form = $form;
            $this->renderForm();
            return;
        }
        if (!$form->isConfirmed()) {
            return;
        }
        $code = $form->getElement('id')->getValue();
        $daoOffer = new Application_Model_DAO_Adv();
        $daoOffer->deactivate($code, 'code');

        $this->addSuccess($this->translate('XXX_ADMIN_ADV_DEACTIVATE_SUCCESS'));
        $url = $this->url(array('controller' => 'adv', 'action' => 'index'));
        $this->_redirect($url);
    }

    public function activateAction() {
        $code = $this->_getParam('id');
        $form = new Admin_Form_Confirmation();

        if (!$this->_request->isPost() || !$form->isValid($_POST)) {
            if (!$this->_request->isPost()) {
                $form->getElement('id')->setValue($code);
            }
            $this->view->form = $form;
            $this->renderForm();
            return;
        }
        if (!$form->isConfirmed()) {
            return;
        }
        $code = $form->getElement('id')->getValue();
        $daoOffer = new Application_Model_DAO_Adv();
        $daoOffer->activate($code, 'code');

        $this->addSuccess($this->translate('XXX_ADMIN_ADV_ACTIVATE_SUCCESS'));
        $url = $this->url(array('controller' => 'adv', 'action' => 'index'));
        $this->_redirect($url);
    }

    public function saveAction() {
        $code = $this->_getParam('id');
        $isEdit = false;
        if (!empty($code)) {
            $daoAdv = new Application_Model_DAO_Adv();
            $record = $daoAdv->getByCode($code);
            d($record);
            $form = new Admin_Form_Adv_Edit();
            if (!$this->_request->isPost()) {
                $record['id'] = $code;
                $form->populate($record);
            }
            $isEdit = true;
        } else {
            $form = new Admin_Form_Adv_Add();
        }
        if (!$this->_request->isPost() || !$form->isValid($_POST)) {
            $this->view->form = $form;
            $this->renderForm();
            return;
        }
        $data = $form->getValues();
        unset($data['id']);
        $daoAdv = new Application_Model_DAO_Adv();
        if ($isEdit) {
            $record = $daoAdv->update($record['adv_id'], $data);
        } else {
            $record = $daoAdv->addNew($data);
        }

        $this->addSuccess($this->translate('XXX_ADMIN_ADV_ADD_SUCCESS'));
        $url = $this->url(array('controller' => 'adv', 'action' => 'index'));
        $this->_redirect($url);
    }

}

